package com.kgc.crowd.distributioncrowddatabaseprovider.controller;

import com.kgc.crowd.distributioncrowdcommon.pojo.MemberLaunchInfoPO;
import com.kgc.crowd.distributioncrowdcommon.pojo.MemberPO;
import com.kgc.crowd.distributioncrowdcommon.pojo.ResultEntity;
import com.kgc.crowd.distributioncrowdcommon.util.CrowdConstant;
import com.kgc.crowd.distributioncrowdcommon.util.CrowdUtils;
import com.kgc.crowd.distributioncrowddatabaseprovider.service.MemberService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RequestMapping("/database/provider")
@RestController
public class MemberController {
    @Resource
    MemberService memberService;

    @RequestMapping("/retrieve/loign/acct/count")
    public ResultEntity<Integer> retrieveLoignAcctCount(
            @RequestParam("loginAcct") String loginAcct) {

        if(!CrowdUtils.strEffectiveCheck(loginAcct)) {
            return ResultEntity.failed(CrowdConstant.MESSAGE_LOGINACCT_INVALID);
        }

        try {
            int count = memberService.getLoignAcctCount(loginAcct);

            return ResultEntity.successWithData(count);

        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }
    }

    @RequestMapping("/save/member/remote")
    public ResultEntity<String> saveMemberRemote(@RequestBody MemberPO memberPO) {

        try {
            // 执行保存
            memberService.saveMemberPO(memberPO);
        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }

        return ResultEntity.successNoData();
    }
    @RequestMapping("/retrieve/member/by/login/acct")
    public ResultEntity<MemberPO> retrieveMemberByLoginAcct(@RequestParam("loginAcct") String loginAcct) {

        MemberPO memberPO = null;

        try {
            memberPO = memberService.getMemberByLoginAcct(loginAcct);
        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }

        return ResultEntity.successWithData(memberPO);
    }
    @RequestMapping("/retrieve/member/launch/info/po")
    public ResultEntity<MemberLaunchInfoPO> retrieveMemberLaunchInfoPO(@RequestParam("memberId") Integer memberId){
        try{
            MemberLaunchInfoPO memberLaunchInfoPO = memberService.getMemberLaunchInfoPOByMember(memberId);
            return ResultEntity.successWithData(memberLaunchInfoPO);
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }
    }
}
